package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.component.OperLog;
import com.zygh.policeasset.pojo.User;
import com.zygh.policeasset.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/user"})
@Api(tags = {"用户管理模块"})
@RestController
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/controller/UserController.class */
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired(required = false)
    private PasswordEncoder passwordEncoder;

    @RequestMapping(value = {"/findAllUser"}, method = {RequestMethod.GET})
    @ApiOperation("查询所有用户")
    public Object findAllUser(int pageNum, int pageSize,
                              @RequestParam(value = "name", required = false) String name,
                              @RequestParam(value = "departmentId", required = false) Integer departmentId) {
        return this.userService.findAllUser(pageNum, pageSize, name, departmentId);
    }

    @RequestMapping(value = {"/findAllUserNoPage"}, method = {RequestMethod.GET})
    @ApiOperation("查询所有用户")
    public Object findAllUserNoPage(@RequestParam(value = "name", required = false) String name) {
        return this.userService.findAllUserNoPage(name);
    }

    @RequestMapping(value = {"/findUser"}, method = {RequestMethod.GET})
    @ApiOperation("查询个人信息")
    public Object findUser(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "departmentId", required = false) Integer departmentId) {
        return this.userService.findUser(name, departmentId);
    }

    @RequestMapping(value = {"/addUser"}, method = {RequestMethod.POST})
    @ApiOperation("添加用户")
    public Object addUser(@RequestBody User user) {
        if (StrUtil.isEmpty(user.getName())) {
            return new CommonResult().validateFailed("用户姓名不能为空");
        }
        if (StrUtil.isEmpty(user.getUsername())) {
            return new CommonResult().validateFailed("用户名不能为空");
        }
        if (this.userService.findByUsername(user.getUsername()) != null) {
            return new CommonResult().validateFailed("用户名已存在");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String password = user.getPassword();
        if (StrUtil.isEmpty(password)) {
            password = "888888";
        }
        user.setPassword(passwordEncoder.encode(password));
        user.setSignaturePassword(passwordEncoder.encode(password));
        if (this.userService.addUser(user).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加用户错误");
    }

    @RequestMapping(value = {"/updateUser"}, method = {RequestMethod.POST})
    @ApiOperation("修改用户")
    public Object updateUser(@RequestBody User user) {
        if (!StrUtil.isEmpty(user.getPassword())) {
            user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        }
        if (this.userService.updateUser(user).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改用户错误");
    }

    @RequestMapping(value = {"/deleteUser"}, method = {RequestMethod.POST})
    @ApiOperation("删除用户")
    public Object deleteUser(@RequestBody User user) {
        if (this.userService.deleteUser(user.getId()).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除用户错误");
    }

    @RequestMapping(value = {"/updatePassword"}, method = {RequestMethod.POST})
    @ApiOperation("修改用户密码")
    public Object updatePassword(@RequestBody User user) {
        if ("888888".equals(user.getNewPassWord())) {
            return new CommonResult().validateFailed("密码不能设置为默认密码");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        User user1 = this.userService.findByUsername(user.getUsername());
        if (!passwordEncoder.matches(user.getOldPassWord(), user1.getPassword())) {
            return new CommonResult().validateFailed("原密码错误");
        }
        if (this.userService.updateByUserNP(Integer.valueOf(user1.getId()), passwordEncoder.encode(user.getNewPassWord()), null) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改用户错误");
    }

    @RequestMapping(value = {"/updateFirstTimeLogin"}, method = {RequestMethod.POST})
    @ApiOperation("首次登陆修改密码")
    public Object updateFirstTimeLogin(@RequestBody User user) {
        if ("888888".equals(user.getNewPassWord())) {
            return new CommonResult().validateFailed("密码不能设置为默认密码");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        User user1 = this.userService.findByUsername(user.getUsername());
        if (StringUtils.isEmpty(user1)) {
            return new CommonResult().validateFailed("系统中无此用户");
        }
        if (this.userService.updateByUserNP(Integer.valueOf(user1.getId()), passwordEncoder.encode(user.getNewPassWord()), 0) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改密码失败");
    }

    @RequestMapping(value = {"/updatePasswordSignature"}, method = {RequestMethod.POST})
    @ApiOperation("修改签名密码")
    public Object updatePasswordSignature(@RequestBody User user) {
        if ("888888".equals(user.getNewSignaturePassword())) {
            return new CommonResult().validateFailed("密码不能设置为默认密码");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        User user1 = this.userService.findByUserId(Integer.valueOf(user.getId()));
        if (!passwordEncoder.matches(user.getSignaturePassword(), user1.getSignaturePassword())) {
            return new CommonResult().validateFailed("原密码错误");
        }
        if (this.userService.updateByUserNP(Integer.valueOf(user1.getId()), passwordEncoder.encode(user.getNewSignaturePassword()), null) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改签名密码错误");
    }

    @RequestMapping(value = {"/updateFirstTimeSignature"}, method = {RequestMethod.POST})
    @ApiOperation("首次修改签名密码")
    public Object updateFirstTimeSignature(@RequestBody User user) {
        if ("888888".equals(user.getNewSignaturePassword())) {
            return new CommonResult().validateFailed("密码不能设置为默认密码");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        User user1 = this.userService.findByUsername(user.getUsername());
        if (StringUtils.isEmpty(user1)) {
            return new CommonResult().validateFailed("系统中无此用户");
        }
        if (this.userService.updateByUserNP(Integer.valueOf(user1.getId()), passwordEncoder.encode(user.getNewSignaturePassword()), 0) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("首次修改签名密码失败");
    }

    @RequestMapping(value = {"/resetPassword"}, method = {RequestMethod.POST})
    @Transactional(rollbackFor = {Exception.class})
    @ApiOperation("管理员重置用户密码")
    public Object resetPassword(@RequestBody JSONObject json) {
        try {
            String password = new BCryptPasswordEncoder().encode("888888");
            int count = 0;
            for (String id : json.get("ids").toString().split(",")) {
                this.userService.updateByUserNP(Integer.valueOf(Integer.parseInt(id)), password, 1);
                count++;
            }
            if (count > 0) {
                return new CommonResult().success();
            }
            return new CommonResult().validateFailed("重置密码失败");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    @RequestMapping(value = {"/signature"}, method = {RequestMethod.POST})
    @ApiOperation("签名设置")
    public Object signature(@RequestBody Map<String, Object> map) throws IOException {
        return this.userService.signature(map.get("base64").toString(), Integer.valueOf(Integer.parseInt(map.get("userId").toString())));
    }

    @RequestMapping(value = {"/selectSignature"}, method = {RequestMethod.GET})
    @ApiOperation("查询签名")
    public Object selectSignature(@RequestParam("userId") Integer userId) {
        return this.userService.selectSignature(userId);
    }

    @RequestMapping(value = {"/selectSignatureTime"}, method = {RequestMethod.GET})
    @ApiOperation("查询最后一次签名时间")
    public Object selectSignatureTime(@RequestParam("userId") Integer userId) {
        return this.userService.selectSignatureTime(userId);
    }

    @RequestMapping(value = {"/updateOaidById"}, method = {RequestMethod.POST})
    @ApiOperation("登录设备解绑")
    public Object updateOaidById(@RequestBody JSONObject json) {
        return this.userService.updateUuidById(json.getInteger("id"));
    }

    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = CommonResult.class)})
    @RequestMapping(value = {"/SignatureCheck"}, method = {RequestMethod.POST})
    @ApiOperation("签名密码校验")
    @OperLog(operModul = "签名密码校验", operType = "查询", operDesc = "签名密码校验")
    public Object SignatureCheck(@RequestBody User params) {
        if (StrUtil.isEmpty(params.getUsername())) {
            return new CommonResult().validateFailed("用户名不能为空");
        }
        if (StrUtil.isEmpty(params.getSignaturePassword())) {
            return new CommonResult().validateFailed("签名密码不能为空");
        }
        User user1 = this.userService.findByUsername(params.getUsername());
        if (!this.passwordEncoder.matches(params.getSignaturePassword(), user1.getSignaturePassword())) {
            return new CommonResult().validateFailed("账号密码不匹配");
        }
        this.userService.updateSignatureLastTime(Integer.valueOf(user1.getId()));
        return new CommonResult().success();
    }
}